package com.grj.park.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grj.park.boot.domain.Dictionary;
import com.grj.park.boot.service.DictionaryService;
import com.grj.park.boot.mapper.DictionaryMapper;
import com.grj.park.boot.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author eren
* @description 针对表【dictionary】的数据库操作Service实现
* @createDate 2024-10-21 15:45:24
*/
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary>
    implements DictionaryService{



    @Override
    public Page<Dictionary> fetchDictionaryPage(int currentPage, int pageSize, Dictionary dictionary, List<Date> during) {
        QueryWrapper<Dictionary> dictionaryQueryWrapper = Wrappers.query();
        if (during != null && !during.isEmpty()) {
            dictionaryQueryWrapper.between("create_time", during.get(0), during.get(1));
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(dictionary);
        HashMap<String, Object> map = new HashMap<>();
        stringObjectMap.forEach((k, v) -> {
            if (v != null) {
                map.put(StringUtil.toCamelCase(k), v);
            }
        });
        dictionaryQueryWrapper.allEq(map, false);
        return baseMapper.selectPage(new Page<>(currentPage, pageSize), dictionaryQueryWrapper);
    }

    @Override
    public void postDictionary(Dictionary dictionary) {
        baseMapper.insert(dictionary);
    }

    @Override
    public void putDictionary(Dictionary dictionary) {
        baseMapper.updateById(dictionary);
    }

    @Override
    public void delDictionary(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void batchDelDictionary(List<Long> ids) {
        QueryWrapper<Dictionary> dictionaryQueryWrapper = Wrappers.query();
        ids.forEach(id -> {
            dictionaryQueryWrapper.or().eq("id", id);
            dictionaryQueryWrapper.or().eq("parent_id", id);
        });
        baseMapper.delete(dictionaryQueryWrapper);
    }

    @Override
    public Dictionary fetchDictionaryById(Long id) {
        return baseMapper.selectById(id);
    }


}




